/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.identity.naive.internal;

import org.osami.gateway.identity.Identity;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

import static org.testng.Assert.*;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 29 juil. 2010
 * Time: 13:20:18
 * To change this template use File | Settings | File Templates.
 */
public class DefaultIdentityTestCase {

    @Test
    public void testIdentityName() throws Exception {
        DefaultIdentity id1 = new DefaultIdentity("1234-5678-90");
        assertNull(id1.getName(), "Name should not been set at creation time");
        assertEquals(id1.getIdentityID(), "1234-5678-90");

        // --------------------

        Map<String, Object> props = new HashMap<String, Object>();
        props.put(Identity.FRIENDLY_NAME, "Guillaume");

        DefaultIdentity id2 = new DefaultIdentity("1234-5678-90");
        id2.putProperties(props);
        assertEquals(id2.getName(), "Guillaume");
        assertEquals(id2.getIdentityID(), "1234-5678-90");

        // --------------------

        Map<String, Object> props2 = new HashMap<String, Object>();
        props2.put(Identity.FRIENDLY_NAME, "Guillaume");

        DefaultIdentity id3 = new DefaultIdentity("1234-5678-90");
        id3.putProperties(props2);
        assertEquals(id3.getName(), "Guillaume");
        assertEquals(id3.getIdentityID(), "1234-5678-90");
    }

    @Test
    public void testBoundUnboundIdentity() throws Exception {
        DefaultIdentity id1 = new DefaultIdentity("1234-5678-90");
        assertNull(id1.getBoundIdentifier());
        assertEquals(id1.getState(), Identity.State.UNBOUND);

        id1.setBoundIdentifier("abcd-ef");
        assertEquals(id1.getBoundIdentifier(), "abcd-ef");
        assertEquals(id1.getState(), Identity.State.BOUND);
    }

    @Test
    public void testParentLinkage() throws Exception {
        DefaultIdentity parent = new DefaultIdentity("1234-5678-90");
        parent.setName("Parent");

        assertTrue(parent.getChildren().isEmpty());

        DefaultIdentity child1 = new DefaultIdentity("1234-5678-90-child-1");
        child1.setName("Child-1");
        assertNull(child1.getParent());

        child1.setParent(parent);

        assertNotNull(child1.getParent());
        assertEquals(parent.getChildren().size(), 1);

        DefaultIdentity child2 = new DefaultIdentity("1234-5678-90-child-2");
        child2.setName("Child-2");
        assertNull(child2.getParent());

        child2.setParent(parent);

        assertNotNull(child2.getParent());
        assertEquals(parent.getChildren().size(), 2);

    }

    @Test
    public void testParentInheritance() throws Exception {

        Map<String, Object> parentProps = new HashMap<String, Object>();
        parentProps.put("a", "1");
        parentProps.put("c", "3");

        DefaultIdentity parent = new DefaultIdentity("1234-5678-90");
        parent.setName("Parent");
        parent.putProperties(parentProps);

        Map<String, Object> childProps = new HashMap<String, Object>();
        childProps.put("b", "2");

        DefaultIdentity child1 = new DefaultIdentity("1234-5678-90-child");
        child1.setName("Child");
        child1.setParent(parent);
        child1.putProperties(childProps);

        Map<String, Object> merged = child1.getMergedProperties();
        assertTrue(merged.containsKey("a"));
        assertTrue(merged.containsKey("b"));
        assertTrue(merged.containsKey("c"));
    }

    @Test
    public void testParentValuesOverriding() throws Exception {

        Map<String, Object> parentProps = new HashMap<String, Object>();
        parentProps.put("overridden", false);

        DefaultIdentity parent = new DefaultIdentity("1234-5678-90");
        parent.setName("Parent");
        parent.putProperties(parentProps);

        Map<String, Object> childProps = new HashMap<String, Object>();
        childProps.put("overridden", true);

        DefaultIdentity child1 = new DefaultIdentity("1234-5678-90-child");
        child1.setName("Child");
        child1.setParent(parent);
        child1.putProperties(childProps);

        Map<String, Object> merged = child1.getMergedProperties();
        assertTrue((Boolean) merged.get("overridden"));
    }

    @Test
    public void testPropertySet() throws Exception {
        DefaultIdentity identity = new DefaultIdentity("1234-5678-90");
        identity.addProperty("one", "two");

        assertEquals(identity.getProperties().get("one"), "two");


    }
}
